-- ship_gun_skill
-- create by xinj
-- 技能: 施放N颗流星
-- 目前支持如下两种配置：
-- ship_gun_skill(600,5)：施放5颗炮，每颗600威力的魔法伤害
-- ship_gun_skill(600,5,style,2000)：施放5颗炮，每颗600威力的魔法伤害，如果目标是style系，则效果增强200%


return {
    apply = function(source, target, skillId, round, para, extra)
        -- 威力，数量
        local num = para[2];
        local baseSkill = SkillM.findBaseSkill(skillId);

        local combine = PropM.combine;
        local PropM_apply = PropM.apply;
        local fetchProps = PropM.fetchProps;
        local trigger = PropM.trigger;

        --威力
        local base = para[1];
        local force = para[1];

        -- 2. 攻击方的魔力
        local magic = source:getMagic();

        -- 被对方百分比削弱
        local prop = combine(target, "weak_enemy", "magic");
        magic = PropM_apply(prop, magic);

        -- 被对方绝对值削弱
        prop = combine(target, "weak_enemy2", "magic");
        magic = PropM_apply(prop, magic);

        -- 3. 伤害
        local damage = math.modf(force * magic / 1000);

        -- 数量增加
        local props = fetchProps(source, "skill_goal_add");
        for _, prop in ipairs(props) do
            if tonumber(prop[2]) == baseSkill then
                num = PropM_apply(prop, num);
            end
        end

        -- 连续打击目标
        local pos;
        local grid;
        local monster;
        local targetPosList = {};

        for i = 1, num do
            local hasTarget = false;
            for pos = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
                grid = DungeonM.getGridByPos(pos);

                -- 翻开且存活的怪物
                if grid:isOpened() and
                   grid:isMonster() and
                   not grid.monster:isDead() then
                    monster = grid.monster;
                    hasTarget = true;

                    -- 将怪物的位置添加到列表中
                    table.insert(targetPosList, pos);

                    break;
                end
            end

            -- 找不到怪物就结束
            if not hasTarget then
                break;
            end

            local finalDamage = damage;
            if #para == 4 and tonumber(para[3]) == monster.style then
                -- 该系别有伤害加成,需重新计算伤害
                finalDamage = math.modf((force + base * para[4] / 1000) * magic / 1000);
            end

            -- 魔法暴击类，多种暴击线性叠加伤害
            local addon_damage = 0;

            -- 伤害类魔法概率增强
            props = fetchProps(source, "damage_magic");
            for _, prop in ipairs(props) do
                addon_damage = addon_damage + trigger(source, prop[1], prop[2], finalDamage);
            end
            -- 魔法暴击
            props = fetchProps(source, "critical_magic");
            for _, prop in ipairs(props) do
                addon_damage = addon_damage + trigger(source, prop[1], prop[2], finalDamage);
            end
            -- 概率双倍
            props = fetchProps(source, "skill_double");
            for _, prop in ipairs(props) do
                if tonumber(prop[2]) == skillId then
                    addon_damage = addon_damage + trigger(source, prop[1], prop[2], finalDamage);
                end
            end
            -- 最终伤害
            finalDamage = finalDamage + addon_damage;

            -- 魔法抗性
            prop = combine(monster, "resist", "magic");
            -- 忽视抗性
            local p = combine(source, "ignore_resist", baseSkill);
            prop[3] = PropM_apply(p, prop[3]);
            p = combine(source, "ignore_resist", "magic");
            prop[3] = PropM_apply(p, prop[3]);

            finalDamage = PropM_apply(prop, finalDamage);

            -- 增加命中动作
            SkillM.initSequence(source, monster, skillId);
            -- if SkillM.getSequence(monster) == nil then
            --     SkillM.getSequenceList()[monster] = CastSequence.create(source, monster, skillId);
            -- end

            SkillM.getSequence(monster):hit(source, monster, skillId);

            -- 受创
            CombatM.receiveDamage(source, monster, finalDamage, skillId, extra);
        end

        if #targetPosList > 0 then
            -- 抛出事件
            EventMgr.fire(event.USE_SHIP_SKILL, { ["skillId"] = skillId, ["posList"] = targetPosList,});
        end

        return { monster, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");

        if #arr == 2 then
            -- 规则1：star_damage(威力,数量)
            return { tonumber(arr[1]), tonumber(arr[2]) };
        else
            -- 规则2：area_damage(威力,数量,style,2000)
            return { tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3]), tonumber(arr[4]), };
        end
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        local base = para[1];
        local num   = para[2];
        local who = source.ship;
        local baseSkill = SkillM.findBaseSkill(skillId);

        if not who then
            who = source;
        end

        -- 暂无加成
        local force = para[1];

        -- 显示数量增加
        local props = PropM.fetchProps(who, "skill_goal_add");
        for _, prop in pairs(props) do
            if tonumber(prop[2]) == baseSkill then
                num = PropM.apply(prop, num);
            end
        end

        -- 2. 攻击方的魔力
        local magic = who:getMagic();

        -- 3. 伤害
        local damageBase = math.modf(base * magic / 1000);
        local damageForce = math.modf(force * magic / 1000);
        local damageAddon = damageForce - damageBase;
        if damageAddon == 0 then
            desc = string.gsub(desc, "{ship_gun_skill}", damageForce);
        else
            desc = string.gsub(desc, "{ship_gun_skill}", damageBase .. "(+" .. damageAddon .. ")");
        end

        desc = string.gsub(desc, "{num}", num);

        -- 系别TODO

        return desc;
    end,
}
